package com.landray.kmss.km.review.webservice;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 洪小才
 * @Description
 * @Date Created in 19:52  2018-06-26
 * @Modified By
 **/
public class Util {
    /**
     * 创建文档及流程数据
     */
    KmReviewParamterForm createForm() throws Exception, IOException {
        KmReviewParamterForm form = new KmReviewParamterForm();

        // 文档模板id
        form.setFdTemplateId("1643592606668dbd3a689fe40dfbf352");

        // 文档标题
        form.setDocSubject("现场督办");

        form.setDocStatus("20");

        // 流程发起人
        form.setDocCreator("{\"LoginName\": \"admin\"}");

        //文档关键字
        //form.setFdKeyword("\"立项\"");

        // 流程表单
        String formValues = "{\"fd_3657f72688db1c\":\"段聪聪\", \"fd_3657f73a332dea\":\"督办立项\", \"fd_3657f73cb88ece\":\"洪小才测试\", \"fd_3657f73cb88ece\":\"督办\"}";
        form.setFormValues(formValues);

        // 流程参数
        //String flowParam = "{auditNode:\"请审核\", futureNodeId:\"N3\", changeNodeHandlers:[\"N3:1641b129473a9e6c34249bf417aaa911\"]}";
        //form.setFlowParam(flowParam);

        //List<AttachmentForm> attForms = createAllAtts();
        //form.getAttachmentForms().addAll(attForms);
        //form.getAttachmentForms().addAll(null);

        return form;
    }

    /**
     * 创建附件列表
     */
    List<AttachmentForm> createAllAtts() throws Exception, IOException {

        List<AttachmentForm> attForms = new ArrayList<AttachmentForm>();

        String fileName = "采购订单说明书1.doc";
        AttachmentForm attForm01 = createAtt(fileName);
        fileName = "采购订单说明书2.doc";
        AttachmentForm attForm02 = createAtt(fileName);

        attForms.add(attForm01);
        attForms.add(attForm02);

        return attForms;
    }

    /**
     * 创建附件对象
     */
    AttachmentForm createAtt(String fileName) throws Exception, IOException {
        AttachmentForm attForm = new AttachmentForm();
        attForm.setFdFileName(fileName);
        // 设置附件关键字，表单模式下为附件控件的id
        attForm.setFdKey("fd_2eddbf161a3268");

        byte[] data = file2bytes("C:\\" + fileName);
        DataHandler dataHandler = createDataHandler(data);
        attForm.setFdAttachment(dataHandler);

        return attForm;
    }

    /**
     * 将文件转换为字节编码
     */
    byte[] file2bytes(String fileName) throws Exception, IOException {
        InputStream in = new FileInputStream(fileName);
        byte[] data = new byte[in.available()];

        try {
            in.read(data);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
            }
        }

        return data;
    }

    public static DataHandler createDataHandler(final byte[] b) {
        return new DataHandler(new DataSource() {
            public InputStream getInputStream() {
                return new ByteArrayInputStream(b);
            }

            public OutputStream getOutputStream() {
                return null;
            }

            public String getContentType() {
                return "";
            }

            public String getName() {
                return "";
            }
        });
    }
}
